//
// copyright 2003 Don Kackman - mailto:dkackman_2000@yahoo.com
//
// no warranty expressed or implied
// use however you'd like
//
using System;

namespace VersionVDProj
{
    /// <summary>
    /// This is the class that has the Main method. This class is responsible for parsing command line
    /// arguments and dispatching them to the correct classes
    /// </summary>
    class App
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main(string[] args)
        {
            if (args.Length > 1)
            {
                try
                {
                    ArgumentParser arguments = new ArgumentParser(args);
                    VDProjectVersioner versioner = null;

                    switch (arguments.Command)
                    {
                        case "msi":
                            versioner = new MSIProjectVersioner(arguments.ProjectFile);
                            break;
                        case "msm":
                            versioner = new MSMProjectVersioner(arguments.ProjectFile);
                            break;
                        default:
                            throw new Exception(string.Format("Unrecognized command {0}", arguments.Command));
                    }

                    versioner.UpdateFile(arguments.Options);
                    return 0;
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR");
                    Console.WriteLine(e.Message);
                    return 1;
                }
            }

            // with no arguments show the usage
            ShowUsage();
            return 0;
        }

        private static void ShowUsage()
        {
            Console.WriteLine("VersionVDProj -msi | -msm projectPath version=<VERSION>");
            Console.WriteLine("[packagecode=<PACKAGECODE>] [productcode=<PRODUCTCODE>]");
            Console.WriteLine("[upgradecode=<UPGRADECODE>] [signature=<SIGNATURE>] [outfile=<OUTPATH>]");
            Console.WriteLine("");
            Console.WriteLine("-msi | -msm\tSpecifies whether projectPath is a MSI or MSM project");
            Console.WriteLine("projectPath\tSpecifies the location of the .vdproj file");
            Console.WriteLine("version\t\tSpecifies the new version to assign to the project");
            Console.WriteLine("\t\tCan be 1 of 2 formats:");
            Console.WriteLine("\t\t1) The path to an assembly who's version will be used");
            Console.WriteLine("\t\t2) A 3 part version where each field can be ? | + | [number]");
            Console.WriteLine("\t\tif ? the current value from the project for that field will");
            Console.WriteLine("\t\t\tbe preserved");
            Console.WriteLine("\t\tif + the current value from the project for that field will");
            Console.WriteLine("\t\t\tbe incremented by 1");
            Console.WriteLine("\t\tif [number] that field will be set to the value specified");
            Console.WriteLine("\t\tExamples of versions");
            Console.WriteLine("\t\t\tversion=1.0.0 set's the version to this value");
            Console.WriteLine("\t\t\tversion=?.?.+ leaves the first 2 fields and increments");
            Console.WriteLine("\t\t\t\tthe last field by 1");
            Console.WriteLine("packagecode\tOptional package guid (if not specified will be auto-generated)");
            Console.WriteLine("\t\tonly applies to -msi");
            Console.WriteLine("productcode\tOptional product guid (if not specified will be set to the same");
            Console.WriteLine("\t\tvalue used to set the package code)");
            Console.WriteLine("\t\tonly applies to -msi");
            Console.WriteLine("upgradecode\tOptional upgrade guid (if not specified the upgrade code will");
            Console.WriteLine("\t\tnot be modified)");
            Console.WriteLine("\t\tonly applies to -msi");
            Console.WriteLine("signature\tOptional unique id for the merge module (if not specified a");
            Console.WriteLine("\t\tguid based signature will be auto-generated)");
            Console.WriteLine("\t\tonly applies to -msm");
            Console.WriteLine("outfile\t\tSpecifies the path to the output file to create. If not");
            Console.WriteLine("\t\tspecified the input project will be modified in place");
            Console.WriteLine("");
            Console.WriteLine("Named arguments can be specified in any order.");
        }
    }
}
